from DB import *
from Actions import *
from Tools import *
from GetInput import *
from MenuUI import *
from AOFManager import *
import Global
import copy
import time
import os.path

path = os.path.abspath(__file__)
dirname, useless = os.path.split(path)
Global.rootDir = dirname + '/'

print('\nLOADING... PLEASE WAIT...')
Global.db = DB(Global.rootDir + Global.dbPath)
if Global.db.init() != 0 :
    print("ERROR: CREATE / OPEN DATABASE FAILED.")
    exit(1)
Global.categorys = Global.db.getCategorys()
if Global.categorys == None :
    print("get category failed.")
    exit(1)
Global.accounts = Global.db.getAccounts()
Global.aofManager = AOFManager(Global.rootDir + Global.aofName)
    
mainMenu = MenuUI("MAIN MENU")
m1 = MenuUI("TAKE A RECORD")
m1.addSubMenu(MenuUI("INCOME", recordIncomAction))
m1.addSubMenu(MenuUI("PAYMENT", recordPaymentAction))
m1.addSubMenu(MenuUI("TRANSFER", recordTransferAction))
# m2 = MenuUI("BRIEF")
m3 = MenuUI('STATISTIC')
m31 = MenuUI('PROPERTY OVERVIEWS', popertyOverviewsAction)
m32 = MenuUI('INCOME & PAYMENT OVERVIEWS')
m321 = MenuUI('BY DAY', IPOverviewsByDayAction)
m322 = MenuUI('BY MONTH', IPOverviewsByMonthAction)
m323 = MenuUI('BY YEAR', IPOverviewsByYearAction)
m32.addSubMenu(m321)
m32.addSubMenu(m322)
m32.addSubMenu(m323)
# m33 = MenuUI('INCMOE CAT1 PERCENTAGE')
# m34 = MenuUI('PAYMENT CAT1 PERCENTAGE')
# m35 = MenuUI('PAYMENT CAT2 PERCENTAGE')
m3.addSubMenu(m31)
m3.addSubMenu(m32)
# m3.addSubMenu(m33)
# m3.addSubMenu(m34)
# m3.addSubMenu(m35)
m4 = MenuUI("BUDGET")
m41 = MenuUI("EDIT BUDGET PLAN")
m41.addSubMenu(MenuUI('GENERATE NEW BUDGET CONFIG FILE', generateNewBudgetConfigFileAction))
m41.addSubMenu(MenuUI('LOAD BUDGET CONFIG FILE', loadBudgetConfigFileAction))
m4.addSubMenu(m41)
m4.addSubMenu(MenuUI("SHOW BUDGET PLAN", showBudgetPlanAction))
m4.addSubMenu(MenuUI("SHOW BUDGET USAGE", showBudgetUsageAction))
m5 = MenuUI("SHOW/DELETE RECORDS")
m5.addSubMenu(MenuUI("SHOW ALL", showAllRecordAction))
m5.addSubMenu(MenuUI("SHOW THIS WEEK", showWeekRecordAction))
m5.addSubMenu(MenuUI("SHOW THIS MONTH", showMonthRecordAction))
m5.addSubMenu(MenuUI("SHOW IN RANGE", showRangeRecordAction))
# m6 = MenuUI("CATEGORIES")
# m61 = MenuUI('INCOME')
# m61.addSubMenu(MenuUI('SHOW INCOME CATEGORY', showIncomeCategoryAction))
# m61.addSubMenu(MenuUI('ADD INCOME CATEGORY', addIncomeCategoryAction))
# m61.addSubMenu(MenuUI('DELETE INCOME CATEGORY', deleteIncomeCategoryAction))
# m61.addSubMenu(MenuUI('EDIT INCOME CATEGORY', editIncomeCategoryAction))
# m62 = MenuUI('PAYMENT')
# m62.addSubMenu(MenuUI('SHOW PAYMENT CATEGORY', showPaymentCategoryAction))
# m62.addSubMenu(MenuUI('ADD PAYMENT CATEGORY', addPaymentCategoryAction))
# m62.addSubMenu(MenuUI('DELETE PAYMENT CATEGORY', deletePaymentCategoryAction))
# m62.addSubMenu(MenuUI('EDIT PAYMENT CATEGORY', editPaymentCategoryAction))
# m6.addSubMenu(m61)
# m6.addSubMenu(m62)
# m7 = MenuUI('ACCOUNTS')
m8 = MenuUI("ADVANCED MANAGEMENT")
m81 = MenuUI('DB MANAGEMENT', dbmAction)
m82 = MenuUI('AOF MANAGEMENT')
m821 = MenuUI('REBUILD ALL DATA', rebuildAllDataAction)
# m822 = MenuUI('SHOW AOF INFO')
m82.addSubMenu(m821)
# m82.addSubMenu(m822)
m8.addSubMenu(m81)
m8.addSubMenu(m82)
m9 = MenuUI('HELP')
m9.addSubMenu(MenuUI('ABOUT', aboutAction))

mainMenu.addSubMenu(m1)
# mainMenu.addSubMenu(m2)     # BRIEF
mainMenu.addSubMenu(m3)     # STATISTIC
mainMenu.addSubMenu(m4)     # BUDGET
mainMenu.addSubMenu(m5)     # SHOW/DELETE RECORDS
# mainMenu.addSubMenu(m6)     # CATEGORIES
# mainMenu.addSubMenu(m7)     # ACCOUNTS
mainMenu.addSubMenu(m8)     # ADVANCED MANAGEMENT
mainMenu.addSubMenu(m9)     # HELP

mainMenu.execute()
Global.db.destroy()
print("GOODBYE~~~")

# TODO
#
# BUDGET
# STATISTIC
# BRIEF
# 記錄退款功能
# CATEGORIES
#   新增、刪除項目，使用showTableInPage
#   順便更新SHOW/DELETE RECORDS中分頁顯示表格的實現方法
#   刪除payment_cat1_record的時候要清空對應的預算
# ACCOUNTS
#   重新定義賬戶
#   賬戶包括貨幣種類，賬戶類型等屬性
#   增加、刪除賬戶
# USERS
#
# 替換數據庫里和關鍵字衝突的字段
# 給菜單實例起名字
#
# 限制單個進程實例
# DB事物好像不能用？
# MenuUI增加導航欄
# 年度报表
# 不用计入预算的类别
# 加入對輸入金額最大值的判斷
# 加入幫助文檔，版本等詳細信息
# 調整分類的順序
# 考慮數據庫的PAYMENT_RECORD表中加入CAT1_ID字段
# 起名字的關鍵字黑名單
